Ubuntu Command Lines

Here is a website link to query linux commands.


  1. create new user with home folder

    1
    adduser XXX
  2. sudo privilege: vim /etc/sudoers and add $username ALL=(ALL) ALL at the bottom.

  3. start ssh on the server, the default port is 22

    1
    2
    sudo apt-get install openssh-server
    sudo/etc/init.d/ssh start
  4. list

    1
    2
    3
    list -a #including hidden files
    list -S #arrange by size
    list -t #arrange by time
  5. view text

    1
    2
    3
    cat
    head/tail -n 10 tmp.txt #view the first/last 10 lines
    less #more powerful than more
  6. Change the privilege

    1
    2
    chmod 777 ./
    chmod a+x ./
  7. Check disk or file size

    1
    2
    df -h 
    du ./ --max-depth 2 -h
  8. Compress or uncompress files, refer to this link.

  9. Grep + regular expression

    1
    grep [xyz]
  10. Search file

    1
    2
    3
    4
    5
    locate "keyword" #fast
    find ./ -maxdepth 1 -name "*keyword*"
    find ./ -name "*keyword*" -size +50M -size -100M
    find ./ -name "*keyword*" -mmin -10 #m:modify min:minute
    find ./ -name "*keyword*" -exec rm -r {} \;
  11. Pipe commands

    1
    2
    3
    ls -l | tr -s ' ' | cut -d ' ' -f 2 #tr to truncate space
    ls -l | sort -rnk2 #-r:reverse -n:numerical -k:k-th column
    ls -l | wc -l #count line
  12. xargs:

    1
    2
    3
    cat python/requirements.txt | xargs -L 1 sudo pip install
    find . -name "*.c" | xargs rm -rf
    find . -name '*.c' | xargs grep 'stdlib.h'
  13. alias: temporary alias command

    1
    2
    alias lnew="cd /home/niuli/caffe"
    unalias lnew
  14. export: The export command is one of the bash shell built-in commands, which means it is part of your shell.

    1
    2
    3
    4
    5
    6
    export a=linux.com
    echo $a
    export -n a #remove variable

    printname () { echo "Linuxcareer.com"; }
    export -f printname #export function

    add LD_LIBRARY_PATH

    1
    2
    echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/lib' >> ~/.bashrc
    source ~/.bashrc
  15. shellscript sample

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    #!/bin/bash
    read -p "Please input your first name: "
    echo -e "\nYour full name is: $firstname $lastname"
    test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0
    test -f $filename && filetype="regulare file"
    test -d $filename && filetype="directory"
    [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0

    if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
    echo "OK, continue"
    elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
    echo "Oh, interrupt!"
    else
    echo "I don't know what your choice is"
    fi

    while [ "$yn" != "yes" -a "$yn" != "YES" ]
    do
    read -p "Please input yes/YES to stop this program: " yn
    done

    for animal in dog cat elephant
    do
    echo "There are ${animal}s.... "
    done